package com.hardroid.workload.ui.fragment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.KelonggaranActivity;
import com.hardroid.workload.ui.PenyesuaianActivity;
import com.hardroid.workload.utils.HardroidUtils;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.workcalc.Employer;
import com.hardroid.workload.workcalc.ObsvDay;
import com.hardroid.workload.workcalc.Questionaire;

public class QuestPanelFragment extends Fragment {

	private Employer employer;
	private ObsvDay day;
	// private QuestionairePanelStatusView statusView;

	private TextView dateText;
	private TextView startText;
	private TextView endText;

	public int employerId = -999;
	public int dayId = -999;

	/*
	 * UI Elements
	 */

	// faktor kelonggaran
	private Button faktorHelpButton;

	private Spinner tenagaKeluarSpinner;
	private Spinner sikapKerjaSpinner;
	private Spinner gerakanKerjaSpinner;
	private Spinner kelelahanMataSpinner;
	private Spinner temperaturSpinner;
	private Spinner atmosferSpinner;
	private Spinner keadaanLingkunganSpinner;
	private RadioButton cahayaBaikRadioButton;
	private RadioButton cahayaBurukRadioButton;
	private NumberPicker hambatanPicker;

	private ArrayAdapter<CharSequence> kelelahanMataBaikAdapter;
	private ArrayAdapter<CharSequence> kelelahanMataBurukAdapter;

	// penyesuaian objektif
	private Button penyesuaianHelpButton;

	private Spinner kecepatanKerjaSpinner;
	private Spinner anggotaBadanSpinner;
	private Spinner pedalKakiSpinner;
	private Spinner penggunaanTanganSpinner;
	private Spinner koordinasiMataSpinner;
	private Spinner peralatanSpinner;

	public QuestPanelFragment() {
		// TODO Auto-generated constructor stub
		super();
		Log.i("QuestionairePanel Fragment", "Constructor");
	}

	public QuestPanelFragment(int id, int did) {
		// TODO Auto-generated constructor stub
		super();
		this.employerId = id;
		this.dayId = did;
		Log.i("QuestionairePanel Fragment", "Constructor " + id + " , " + did);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i("QuestionairePanel Fragment", "onCreate" + employerId);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// if (getArguments()!=null){
		Log.i("QuestionairePanel Fragment", "onCreateView " + employerId);
		return inflater
				.inflate(R.layout.fragment_quest_panel, container, false);
		// }else{
		// return super.onCreateView(inflater, container, savedInstanceState);
		// }
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		
		LayoutInflater inflater = getActivity().getLayoutInflater();

		employer = Singleton.currentProject.lEmployer.get(employerId);
		day = employer.sheet.getObservation(dayId);

		// help button buat keterangan faktor kelonggaran
		faktorHelpButton = (Button) getView().findViewById(
				R.id.quest_faktor_help_button);
		
		faktorHelpButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Intent i= new Intent(getActivity(),KelonggaranActivity.class);
//				startActivity(i);
				if (Environment.getExternalStorageState().equals(
						Environment.MEDIA_MOUNTED)) {
					File file = new File(Environment.getExternalStorageDirectory()
							+ "/hardroid/kelonggaran.pdf");
					if (!file.exists()) {
						InputStream stream;
						try {
							stream = getActivity().getAssets().open("kelonggaran.pdf");
							if (stream != null) {

								try {
									OutputStream out = null;
									File fileDir = new File(
											Environment
													.getExternalStorageDirectory(),
											"hardroid");
									fileDir.mkdirs();
									File helpFile = new File(fileDir + "/kelonggaran.pdf");
									if (helpFile.createNewFile()) {
										out = new FileOutputStream(
												Environment
														.getExternalStorageDirectory()
														+ "/hardroid/kelonggaran.pdf");
										copyFile(stream, out);
										stream.close();
										stream = null;
										out.flush();
										out.close();

										try {
											Uri path = Uri.fromFile(helpFile);
											Intent intent = new Intent(
													Intent.ACTION_VIEW);
											intent.setDataAndType(path,
													"application/pdf");
											intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
											startActivity(intent);

										} catch (Exception e) {
											Toast.makeText(
													getActivity(),
													"Tidak ada PDF reader yang ditemukan",
													Toast.LENGTH_SHORT).show();
										}
									} else {
										Toast.makeText(getActivity(),
												"Gagal mengcopy file help",
												Toast.LENGTH_SHORT).show();
									}
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}

						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					} else {
						try {
							Uri path = Uri.fromFile(new File(Environment
									.getExternalStorageDirectory()
									+ "/hardroid/kelonggaran.pdf"));
							Intent intent = new Intent(Intent.ACTION_VIEW);
							intent.setDataAndType(path, "application/pdf");
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(intent);

						} catch (Exception e) {
							Toast.makeText(getActivity(),
									"Tidak ada PDF reader yang ditemukan",
									Toast.LENGTH_SHORT).show();
						}
					}

				} else {
					Toast.makeText(getActivity(),
							"Tidak ada SD card yang ditemukan", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

		// elemen2 untuk faktor kelonggaran
		tenagaKeluarSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_tenaga_keluar);
		sikapKerjaSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_sikap_kerja);
		gerakanKerjaSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_gerakan_kerja);
		kelelahanMataSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_kelelahan_mata);
		temperaturSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_temperatur);
		atmosferSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_atmosfer);
		keadaanLingkunganSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_keadaan_lingkungan);
		hambatanPicker = (NumberPicker) getView().findViewById(
				R.id.quest_hambatan_picker);

		tenagaKeluarSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.tenagaPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		sikapKerjaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.sikapKerjaPos= arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		gerakanKerjaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.gerakanKerPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		kelelahanMataSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.kelelahanMataPos= arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		temperaturSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.temperaturPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		atmosferSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.atmosferPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		keadaanLingkunganSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.keadaanLingPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		

		hambatanPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				// TODO Auto-generated method stub
				day.questionaire.hambatanValue = newVal;
			}
		});
		
		// tenaga adapter
		ArrayAdapter<CharSequence> tenagaAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_tenaga_keluar,
						android.R.layout.simple_spinner_item);
		tenagaAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tenagaKeluarSpinner.setAdapter(tenagaAdapter);
		// sikap kerja adapter
		ArrayAdapter<CharSequence> sikapKerjaAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_sikap_kerja,
						android.R.layout.simple_spinner_item);
		sikapKerjaAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sikapKerjaSpinner.setAdapter(sikapKerjaAdapter);
		// gerakan kerja adapter
		ArrayAdapter<CharSequence> gerakanKerjaAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_gerakan_kerja,
						android.R.layout.simple_spinner_item);
		gerakanKerjaAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		gerakanKerjaSpinner.setAdapter(gerakanKerjaAdapter);
		// kelelahan mata adapter
		// cahaya terang
		kelelahanMataBaikAdapter = ArrayAdapter.createFromResource(
				getActivity(), R.array.quest_kelelahan_mata_terang,
				android.R.layout.simple_spinner_item);
		kelelahanMataBaikAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		kelelahanMataSpinner.setAdapter(kelelahanMataBaikAdapter);
		// cahaya gelap
		kelelahanMataBurukAdapter = ArrayAdapter.createFromResource(
				getActivity(), R.array.quest_kelelahan_mata_gelap,
				android.R.layout.simple_spinner_item);
		kelelahanMataBurukAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// temperatur adapter
		ArrayAdapter<CharSequence> temperaturAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_temperatur,
						android.R.layout.simple_spinner_item);
		temperaturAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		temperaturSpinner.setAdapter(temperaturAdapter);
		// atmosfer adapter
		ArrayAdapter<CharSequence> atmosferAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_atmosfer,
						android.R.layout.simple_spinner_item);
		atmosferAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		atmosferSpinner.setAdapter(atmosferAdapter);
		
		// keadaan lingkungan adapter		
		ArrayAdapter<CharSequence> keadaanLingkunganAdapter = ArrayAdapter
				.createFromResource(getActivity(),
						R.array.quest_keadaan_lingkungan,
						android.R.layout.simple_spinner_item);
		keadaanLingkunganAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		keadaanLingkunganSpinner.setAdapter(keadaanLingkunganAdapter);
		
		
		// radio button buat cahaya baik / buruk
		cahayaBaikRadioButton = (RadioButton) getView().findViewById(
				R.id.quest_cahaya_baik);
		cahayaBurukRadioButton = (RadioButton) getView().findViewById(
				R.id.quest_cahaya_buruk);
		cahayaBaikRadioButton.setChecked(true);
		cahayaBaikRadioButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setKelelahanMataContent(true);
			}
		});
		cahayaBurukRadioButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setKelelahanMataContent(false);
			}
		});
		// number picker buat hambatan tak terhindarkan
		hambatanPicker.setMaxValue(Questionaire.HAMBATAN_MAX_PERCENTAGE);
		hambatanPicker.setMinValue(Questionaire.HAMBATAN_MIN_PERCENTAGE);

		// penyesuaian objektif
		penyesuaianHelpButton = (Button) getView().findViewById(
				R.id.quest_penyesuaian_help_button);
		penyesuaianHelpButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Intent i= new Intent(getActivity(),PenyesuaianActivity.class);
//				startActivity(i);
				if (Environment.getExternalStorageState().equals(
						Environment.MEDIA_MOUNTED)) {
					File file = new File(Environment.getExternalStorageDirectory()
							+ "/hardroid/penyesuaian.pdf");
					if (!file.exists()) {
						InputStream stream;
						try {
							stream = getActivity().getAssets().open("penyesuaian.pdf");
							if (stream != null) {

								try {
									OutputStream out = null;
									File fileDir = new File(
											Environment
													.getExternalStorageDirectory(),
											"hardroid");
									fileDir.mkdirs();
									File helpFile = new File(fileDir + "/penyesuaian.pdf");
									if (helpFile.createNewFile()) {
										out = new FileOutputStream(
												Environment
														.getExternalStorageDirectory()
														+ "/hardroid/penyesuaian.pdf");
										copyFile(stream, out);
										stream.close();
										stream = null;
										out.flush();
										out.close();

										try {
											Uri path = Uri.fromFile(helpFile);
											Intent intent = new Intent(
													Intent.ACTION_VIEW);
											intent.setDataAndType(path,
													"application/pdf");
											intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
											startActivity(intent);

										} catch (Exception e) {
											Toast.makeText(
													getActivity(),
													"Tidak ada PDF reader yang ditemukan",
													Toast.LENGTH_SHORT).show();
										}
									} else {
										Toast.makeText(getActivity(),
												"Gagal mengcopy file help",
												Toast.LENGTH_SHORT).show();
									}
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}

						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					} else {
						try {
							Uri path = Uri.fromFile(new File(Environment
									.getExternalStorageDirectory()
									+ "/hardroid/penyesuaian.pdf"));
							Intent intent = new Intent(Intent.ACTION_VIEW);
							intent.setDataAndType(path, "application/pdf");
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(intent);

						} catch (Exception e) {
							Toast.makeText(getActivity(),
									"Tidak ada PDF reader yang ditemukan",
									Toast.LENGTH_SHORT).show();
						}
					}

				} else {
					Toast.makeText(getActivity(),
							"Tidak ada SD card yang ditemukan", Toast.LENGTH_SHORT)
							.show();
				}
				
			}
		});
		

		kecepatanKerjaSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_kecepatan_kerja);
		anggotaBadanSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_anggota_badan);
		pedalKakiSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_pedal_kaki);
		penggunaanTanganSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_penggunaan_tangan);
		koordinasiMataSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_koordinasi_mata);
		peralatanSpinner = (Spinner) getView().findViewById(
				R.id.quest_spinner_peralatan);

		kecepatanKerjaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.tingkatKecepatanKerjaPos= arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		anggotaBadanSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.anggotaBadanPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		pedalKakiSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.pedalKakiPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		penggunaanTanganSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.penggunaanTanganPos= arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		koordinasiMataSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.koordinasiMataPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		peralatanSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				day.questionaire.peralatanPos = arg2;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
			
		});
		
		// kecepatan kerja adapter
		ArrayAdapter<CharSequence> kecepatanAdapter = ArrayAdapter
				.createFromResource(getActivity(),
						R.array.quest_kecepatan_kerja,
						android.R.layout.simple_spinner_item);
		kecepatanAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		kecepatanKerjaSpinner.setAdapter(kecepatanAdapter);
		// anggota badan adapter
		ArrayAdapter<CharSequence> anggotaBadanAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_anggota_badan,
						android.R.layout.simple_spinner_item);
		anggotaBadanAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		anggotaBadanSpinner.setAdapter(anggotaBadanAdapter);
		// pedal kaki adapter
		ArrayAdapter<CharSequence> pedalKakiAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_pedal_kaki,
						android.R.layout.simple_spinner_item);
		pedalKakiAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		pedalKakiSpinner.setAdapter(pedalKakiAdapter);
		// penggunaan tangan adapter
		ArrayAdapter<CharSequence> penggunaanTanganAdapter = ArrayAdapter
				.createFromResource(getActivity(),
						R.array.quest_penggunaan_tangan,
						android.R.layout.simple_spinner_item);
		penggunaanTanganAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		penggunaanTanganSpinner.setAdapter(penggunaanTanganAdapter);
		// koordinasi mata adapter
		ArrayAdapter<CharSequence> koordinasiMataAdapter = ArrayAdapter
				.createFromResource(getActivity(),
						R.array.quest_koordinasi_mata,
						android.R.layout.simple_spinner_item);
		koordinasiMataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		koordinasiMataSpinner.setAdapter(koordinasiMataAdapter);
		// peralatan adapter
		ArrayAdapter<CharSequence> peralatanAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.quest_peralatan,
						android.R.layout.simple_spinner_item);
		peralatanAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		peralatanSpinner.setAdapter(peralatanAdapter);

		// status
		dateText = (TextView) getView().findViewById(R.id.day_status_date);
		startText = (TextView) getView().findViewById(
				R.id.day_status_start_work);
		endText = (TextView) getView().findViewById(R.id.day_status_end_work);

		dateText.setText(HardroidUtils.dateToSDate(day.obsvDate));
		startText.setText(HardroidUtils.dateToTime(day.workStart));
		endText.setText(HardroidUtils.dateToTime(day.workEnd));

		

		// if (id != 0 || Singleton.currentProject.lEmployer.size() != 0) {
		// Employer employer = Singleton.currentProject.lEmployer.get(id);
		// ObsvDay day = employer.QuestionairePanel.getTodayObservation();
		// day.lTally.add(new Tally());
		// if (day != null) {
		// adapter = new TalliesAdapter(getActivity(), day.lTally);
		// setListAdapter(adapter);
		// }
		//
		// }

		updateContent();
		Log.i("QuestionairePanel Fragment", "onActivityCreated " + employerId);
	}

	public static QuestPanelFragment createInstance(int id, int did) {
		Log.i("QuestionairePanel Fragment", "createInstance " + id + "," + did);
		QuestPanelFragment retval = new QuestPanelFragment(id,
				did);
		Log.i("QuestionairePanel Fragment", "endCreateInstance");
		return retval;
	}

	public void setKelelahanMataContent(boolean isCahayaBaik) {
		Log.i("Questionaire Panel", "Set Cahaya "+isCahayaBaik);
		day.questionaire.cahayaBaik = isCahayaBaik;
		if (cahayaBaikRadioButton.isChecked()) {
			if (isCahayaBaik) {
				Log.i("Questionaire Panel", "Set Mata Baik Adapter");
				kelelahanMataSpinner.setAdapter(kelelahanMataBaikAdapter);
			}else{
				Log.i("Questionaire Panel", "Not Mata Baik Adapter");
			}
		} else {
			if (!isCahayaBaik) {
				Log.i("Questionaire Panel", "Set Mata Buruk Adapter");
				
				kelelahanMataSpinner.setAdapter(kelelahanMataBurukAdapter);
			}else{
				Log.i("Questionaire Panel", "Not Set Mata Buruk Adapter");
			}
		}
	}

	private void copyFile(InputStream in, OutputStream out) throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
	}
	
	public void updateContent() {
		Questionaire q = day.questionaire;
		// faktor kelonggaran
		try {
			tenagaKeluarSpinner.setSelection(q.tenagaPos);
			sikapKerjaSpinner.setSelection(q.sikapKerjaPos);
			gerakanKerjaSpinner.setSelection(q.gerakanKerPos);
			kelelahanMataSpinner.setSelection(q.kelelahanMataPos);
			temperaturSpinner.setSelection(q.temperaturPos);
			atmosferSpinner.setSelection(q.atmosferPos);
			keadaanLingkunganSpinner.setSelection(q.keadaanLingPos);
			hambatanPicker.setValue(q.getHambatanTakTerhindarValue());
			if (q.cahayaBaik) {
				cahayaBaikRadioButton.setChecked(true);
			} else {
				cahayaBurukRadioButton.setChecked(true);
			}
		} catch (Exception e) {
			Log.e("Questionaire Panel", "Kelonggaran");
			e.printStackTrace();
		}

		// penyesuaian objektif
		try {
			kecepatanKerjaSpinner.setSelection(q.tingkatKecepatanKerjaPos);
			anggotaBadanSpinner.setSelection(q.anggotaBadanPos);
			pedalKakiSpinner.setSelection(q.pedalKakiPos);
			penggunaanTanganSpinner.setSelection(q.penggunaanTanganPos);
			koordinasiMataSpinner.setSelection(q.koordinasiMataPos);
			peralatanSpinner.setSelection(q.peralatanPos);
		} catch (Exception e) {
			Log.e("Questionaire Panel", "Penyesuaian");
			e.printStackTrace();
		}
		Log.i("Questionaire Panel", "Updated Content");
	}
}
